<?php

namespace App\Http\Controllers;

use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{


    protected $model;
    public function __construct(UserRepository $user)
    {
        $this->model=$user;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     *
     * user  GET
     * 用户列表
     */
    public function index()
    {

       $result= $this->model->getAllByCondition('status','1');


        return view ('admin.user.index')->with(['result'=>$result]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     *添加用户
     *user/create  GET
     */
    public function create()
    {
       return  view('admin.user.user');

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     *
     * 存储用户信息
     * user   POST
     */
    public function store(Request $request)
    {
        $input=Input::all();
        if($input) {
            $validator = Validator::make($request->input(), [
                'name' => 'required|unique:users,name',
                'password' => 'required|min:6',
                'organize_name'=>'required'
            ], [
                'name.required' => '用户名不能为空',
                'name.unique'=>'用户名已注册',
                'password.required' => '密码不能为空',
                'password.min'=>'密码长度不小于6位',
                'organize_name.required'=>'机构名称不能为空'
            ]);
            if($validator->passes()){
                $input['password']=Crypt::encrypt($input['password']);
                $this->model->store($input);
                return ['msg'=>'用户添加成功'];

            }else{
                return ['msg'=>$validator->errors()->first()];

            }
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * 用户信息展示
     *user/{user}    GET
     */
    public function show($id)
    {

        //获取关联到的文章
        $articles=$this->model->getById($id)->articles->take(10);

        //在获取一次用户信息，此处待研究
        $user=$this->model->getById($id);

        return view('admin.user.detail',['articles'=>$articles,'user'=>$user]);
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *用户信息编辑
     *user/{user}/edit     GET
     */
    public function edit($id)
    {

        $result=$this->model->getById($id);

        $result->password=Crypt::decrypt($result->password);

        return view('admin.user.edit')->with('result',$result);
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * 更新用户信息
     * user/{user}    PUT
     */
    public function update(Request $request, $id)
    {
       $input=Input::all();

        $input['password']=Crypt::encrypt($input['password']);

        if($this->model->update($id,$input)){
            return['status_code'=>200,'msg'=>'更新成功'];
        }else{
            return ['status_code'=>500,'msg'=>'更新失败,请重试'];
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * 删除用户
     * user/{user}    DELETE
     */
    public function destroy($id)
    {
        //判断超管删除行为
        if($id==config('constants.Super_Admin')){
            return ['msg'=>'不能删除自己'];
        }

        if($this->model->setField('id',$id,'status','2')){
            return ['msg'=>'删除成功'];
        }else{
            return ['msg'=>'删除失败,请重试'];
        }
    }
}
